# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from openpyxl import Workbook


class WechatofficalspiderPipeline(object):
    def __init__(self):
        # self.rate = 30
        # self.curate = 0
        self.wb = Workbook()
        self.wa = self.wb.active
        self.wa.append(['时间', '标题', '原文链接', '图片链接'])  # 添加表头

    def process_item(self, item, spider):
        self.wa.append([item['date'], item['title'], item['content'], item['cover']])  # 添加数据
        # self.curate += 1
        # if (self.curate >= self.rate):
        self.wb.save('datas.xlsx')
        return item

    def close_spider(self, spider):
        self.wb.save('datas.xlsx')
